<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

///

class Demand_m extends Base_model {

	function get_fields() {
		return $this -> db -> list_fields('h_demand');
	}

	function get_demand_info($id) {

		$this -> db -> where("demand_id", $id);
		$query = $this -> db -> get("demand");
		return $query;

	}

	function object_match_demand($where__a, $limit = false) {

		$test_flag = false;

		$i = 1;

		$where_str = '';
		$where = '';
		$where_flag = TRUE;

		$record_sql = '';

		
		foreach ($where__a as $name => $value) {

			if ($value != '' AND $value != '0.00' AND $value != '0') {
				if ($name == "sale_total") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= " (   `" . 'total_budget1' . "` <= '" . $value . "' ";
					$where .= " AND `" . 'total_budget2' . "` >= '" . $value . "' )";

				} elseif ($name == 'house_building') {

				} elseif ($name == 'main_building_area') {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= " (    `" . 'indoor_area1' . "` <= '" . $value . "' ";
					$where .= " AND  `" . 'indoor_area2' . "` >= '" . $value . "' ) ";

				} elseif ($name == "stakeholders_land_area") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= " (   `" . 'land_area1' . "` <= '" . $value . "' ";
					$where .= " AND `" . 'land_area2' . "` >= '" . $value . "' )";

				} elseif ($name == "sale_land_unit_price") {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " AND ";
					}

					$where .= " (   `" . 'budget_per_ping1' . "` <= '" . $value . "'    ";
					$where .= " AND `" . 'budget_per_ping2' . "` >= '" . $value . "'  ) ";

				} elseif (strstr($name, "building_area") == true) {

					$vvv = @floor($value);

					if ($where_flag) {

						$where .= " WHERE ";
						$where_flag = false;

					} else {

						$where .= " AND ";

					}

					$where .= " (   `" . 'building_area1' . "` <= '" . $vvv . "' ";
					$where .= " AND `" . 'building_area2' . "` >= '" . $vvv . "' ) ";

				}

			}

		}

		

		

		if ('0 , 15' == $limit) {
			//$this -> db -> insert('h_excerpt_sql', $excerpt_sql_data);
			//$this -> db -> insert('h_pattern', $a_data);
		}

		if ($limit == false) {
			$limit = " 1 , 15 ";
		}

		$CI = &get_instance();
		//$CI->login_lib->users_id();

		//$CI->login_lib->users_name();
		$record_sql;

		/*

		 $this -> db -> insert('h_sql',
		 array(  'SQL' => $record_sql,
		 'type' => 'record demand_m ',
		 'ctime' => time(),
		 'create_id'=>$CI->login_lib->users_id() ,
		 'create_name'=> $CI->login_lib->users_name()));
		 */
		$run_sql = "select * from `h_demand` " . $where;
		//.' limit '.$limit;
		$this -> db -> insert('h_sql', array('SQL' => $run_sql, 'type' => 'from demand_m ', 'ctime' => time(), 'create_id' => $CI -> login_lib -> users_id(), 'create_name' => $CI -> login_lib -> users_name()));

		$this -> set_limit($limit);
		$this -> db -> order_by("ctime", "DESC");
		//$query = $this->db->get("demand");
		$query = $this -> db -> query($run_sql);
		return $query;

	}

	function insert_excerpt_sql($object_id, $_md5, $excerpt_sql, $explanation, $field, $limit) {

		$excerpt_sql_data = array('object_id' => $object_id, 'verify_code' => $_md5, 'obj_dem' => 'object', 'flag' => 'Y', 'excerpt_sql' => $excerpt_sql, 'explanation' => $explanation, 'field' => $field);

		if ('0 , 15' == $limit) {
			$this -> db -> insert('h_excerpt_sql', $excerpt_sql_data);
		}

	}

	function insert_pattern_sql($demand_id, $object_id, $verify_code, $flag, $explanation, $category_name, $compare_number, $main_point, $object_field_name1, $demand_field_name1, $value1, $compare1, $and_or, $object_field_name2, $demand_field_name2, $value2, $compare2, $limit) {

		/*
		 `demand_id`, `object_id`, `verify_code`, `flag`,
		 `explanation`, `category_name`, `compare_number`,
		 `main_point`, `object_field_name1`, `demand_field_name1`,
		 `value1`, `compare1`, `and_or1`,
		 `object_field_name2`, `demand_field_name2`,
		 `value`, `compare2`, `and_or2`

		 */

		$sql_data = array('demand_id' => $demand_id, 'object_id' => $object_id, 'verify_code' => $verify_code, 'flag' => $flag, 'explanation' => $explanation, 'category_name' => $category_name, 'compare_number' => $compare_number, 'main_point' => $main_point, 'object_field_name1' => $object_field_name1, 'demand_field_name1' => $demand_field_name1, 'value1' => $value1, 'compare1' => $compare1, 'and_or' => $and_or, 'object_field_name2' => $object_field_name2, 'demand_field_name2' => $demand_field_name2, 'value2' => $value2, 'compare2' => $compare2);

		if ('0 , 15' == $limit) {
			$this -> db -> insert('h_pattern', $sql_data);
		}

	}

	function excerpt_demand($verify_code) {

		$sql = "SELECT * from `h_excerpt_sql` WHERE `verify_code` = '" . $verify_code . "' AND `flag` = 'Y' ";
		$query = $this -> db -> query($sql);

		$_where_sql = '';
		$_sql_flag = true;

		foreach ($query->result() as $row) {
			if ($_sql_flag) {

				$_sql_flag = false;
				$_where_sql .= ' WHERE ' . $row -> excerpt_sql;

			} else {
				$_where_sql .= ' AND ' . $row -> excerpt_sql;
			}
		}

		$demand_sql = 'SELECT * from `h_demand` ' . $_where_sql;

		$CI = &get_instance();
		$this -> db -> insert('h_sql', array("SQL" => $demand_sql, 'type' => 'excerpt SQL query demand', 'ctime' => time(), 'create_id' => $CI -> login_lib -> users_id(), 'create_name' => $CI -> login_lib -> users_name()));

		$demand_query = $this -> db -> query($demand_sql);
		return $demand_query;

	}

	function excerpt_demand_field($verify_code) {

		$sql = "SELECT distinct field , explanation  from `h_excerpt_sql` WHERE `verify_code` = '" . $verify_code . "' AND `flag` = 'Y' ";
		$query = $this -> db -> query($sql);
		return $query;
	}

	function show_display_demand($demand_id_s) {

		$_whr = '';
		$_whr_flag = true;

		foreach ($demand_id_s as $k => $_v) {

			if ($_whr_flag) {

				$_whr_flag = false;
				$_whr .= " WHERE `demand_id` != '" . $_v . "'";

			} else {

				$_whr .= " AND `demand_id` != '" . $_v . "'";

			}

		}

		$demand_sql = 'SELECT * from `h_demand` ' . $_whr;

		$res = $this -> db -> query($demand_sql);

		return $res;

	}

}
